
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Authorization;
using Yozeev.Common;
using Microsoft.AspNetCore.Mvc;
using Yozeev.SystemConfig;

namespace Yozeev.BusinessLogic
{
    /// <summary>
    /// RechargeRecord应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class RechargeRecordAppService : YozeevAppServiceBase, IRechargeRecordAppService
    {
        private readonly IRepository<RechargeRecord, Guid> _entityRepository;

		private readonly IRepository<TravelAgency,Guid> _travelAgencyRepository;

		private readonly IRepository<PayMethod, Guid> _paymethodRepository;

		private readonly IRechargeRecordManager _entityManager;

		private readonly IQueryHelper _queryHelper;

		/// <summary>
		/// 构造函数 
		///</summary>
		public RechargeRecordAppService(
        IRepository<RechargeRecord, Guid> entityRepository
        ,IRechargeRecordManager entityManager
			,IRepository<TravelAgency, Guid> travelAgencyRepository
			,IRepository<PayMethod, Guid> paymethodRepository
			, IQueryHelper queryHelper
		)
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
			_queryHelper = queryHelper;
			_travelAgencyRepository = travelAgencyRepository;
			_paymethodRepository = paymethodRepository;
        }


        /// <summary>
        /// 获取RechargeRecord的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<RechargeRecordListDto>> GetPaged(GetRechargeRecordsInput input)
		{

            var expression = _queryHelper.WhereExp<RechargeRecord>(input.queryData);
            var query = from entity in _entityRepository.GetAll().AsNoTracking()
                        .Where(expression)
                        select new
                        {
                            entity,
							entity.TravelAgency,
							entity.PayMethod,
						};
            var list = new List<RechargeRecordListDto>();
            query.ToList().ForEach(temp => {
                var _entity = temp.entity.MapTo<RechargeRecordListDto>();
                _entity.TravelAgency = temp.TravelAgency;
                _entity.PayMethod = temp.PayMethod;
                list.Add(_entity);
            });
            var count = list.Count();
			list = list.AsQueryable().OrderBy(input.Sorting)
					.PageBy(input)
					.ToList();
			return new PagedResultDto<RechargeRecordListDto>(count, list);
		}


		/// <summary>
		/// 通过指定id获取RechargeRecordListDto信息
		/// </summary>
		public async Task<RechargeRecordListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<RechargeRecordListDto>();
		}

		/// <summary>
		/// 获取编辑 RechargeRecord
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetRechargeRecordForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetRechargeRecordForEditOutput();
RechargeRecordEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<RechargeRecordEditDto>();

				//rechargeRecordEditDto = ObjectMapper.Map<List<rechargeRecordEditDto>>(entity);
			}
			else
			{
				editDto = new RechargeRecordEditDto();
			}

			output.RechargeRecord = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改RechargeRecord的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateRechargeRecordInput input)
		{

			if (input.RechargeRecord.Id.HasValue)
			{
				await Update(input.RechargeRecord);
			}
			else
			{
				await Create(input.RechargeRecord);
			}
		}


		/// <summary>
		/// 新增RechargeRecord
		/// </summary>
		protected virtual async Task<RechargeRecordEditDto> Create(RechargeRecordEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <RechargeRecord>(input);
            var entity=input.MapTo<RechargeRecord>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<RechargeRecordEditDto>();
		}

		/// <summary>
		/// 编辑RechargeRecord
		/// </summary>
		protected virtual async Task Update(RechargeRecordEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除RechargeRecord信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除RechargeRecord的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出RechargeRecord为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

		/// <summary>
		/// 获取RechargeRecord的分页列表信息--Post方法
		///</summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<PagedResultDto<RechargeRecordListDto>> GetPagedForPost(GetRechargeRecordsInput input)
		{

			var expression = _queryHelper.WhereExp<RechargeRecord>(input.queryData);
			var query = from entity in _entityRepository.GetAll().AsNoTracking()
						.Where(expression)
						select new
						{
							entity,
							entity.TravelAgency,
							entity.PayMethod,
						};
			var list = new List<RechargeRecordListDto>();
			query.ToList().ForEach(temp => {
				var _entity = temp.entity.MapTo<RechargeRecordListDto>();
				_entity.TravelAgency = temp.TravelAgency;
				_entity.PayMethod = temp.PayMethod;
				list.Add(_entity);
			});
			var count = list.Count();
			list = list.AsQueryable().OrderBy(input.Sorting)
					.PageBy(input)
					.ToList();
			return new PagedResultDto<RechargeRecordListDto>(count, list);
		}
	}
}


